use std::io::*;
use std::string::*;

pub struct Data {
    v: Vec<usize>,
    start: usize,
    size: usize,
}

impl Data {
    pub fn new() -> Data {
        Data {
            v: vec![0; 100000],
            start: 0,
            size: 0,
        }
    }

    pub fn push(&mut self, x: usize) {
        self.v[(self.start + self.size) % 100000] = x;
        self.size += 1;
    }

    pub fn top(&self) -> Option<usize> {
        if self.size == 0 {
            None
        } else {
            Some(self.v[self.start])
        }
    }

    pub fn pop(&mut self) {
        if self.size == 0 {
            println!("-1");
        } else {
            if self.start == 99999 {
                self.start = 0;
            } else {
                self.start += 1;
            }
            self.size -= 1;
        }
    }

    pub fn size(&self) -> usize {
        self.size
    }

    pub fn clear(&mut self) {
        self.size = 0;
    }
}

fn main() {
    let stdin = stdin();
    let mut lines = stdin.lock().lines().map(|line| line.unwrap());

    let t: usize = lines.next().unwrap().trim().parse().unwrap(); // 读取测试数据组数

    for _ in 0..t {
        let q: usize = lines.next().unwrap().trim().parse().unwrap(); // 读取每组测试数据的行数
        let mut data = Data::new();
        for _ in 0..q {
            let v: Vec<String> = lines
                .next()
                .unwrap()
                .trim()
                .split_whitespace()
                .map(|str| str.to_string())
                .collect();
            match v[0].as_str() {
                "PUSH" => {
                    let x: usize = v[1].parse().unwrap();
                    data.push(x);
                }
                "TOP" => {
                    if let Some(x) = data.top() {
                        println!("{}", x);
                    } else {
                        println!("-1");
                    }
                }
                "POP" => {
                    data.pop();
                }
                "SIZE" => {
                    println!("{}", data.size());
                }
                "CLEAR" => {
                    data.clear();
                }
                _ => {}
            }
        }
    }
}
